﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;
using System.Data;

public partial class Violation_Create : System.Web.UI.Page
{
    //for leader role
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnCreate.Visible = false;
             if (Session["Permission"] != null)
             {
                 Permission permission = Session["Permission"] as Permission;
                 if (permission.IsViolationNoticeCreate)//违章
                 {
                     btnCreate.Visible = true;
                     
                 }

                 if (Request.Params["uid"] != null)
                 {
                     try
                     {
                         int uid = Convert.ToInt32(Request.Params["uid"]);
                         LoadUser(uid);

                     }
                     catch
                     {
                     }
                 }
             }
        }
    }

    void LoadUser(int uid)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            UserBLL bll = new UserBLL(permission);
            DataSet ds = bll.GetUserView(uid);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                lblCompanyName.Text = SwiftConverter.CovertDBObjectToString(dr["CompanyName"]);
                lblUserName.Text = SwiftConverter.CovertDBObjectToString(dr["UserName"]);
                lblSafe.Text = SwiftConverter.CovertDBObjectToYesOrNo(dr["IsSafetyEducation"]);
            }
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (Session["Permission"] != null && Request.Params["uid"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            int uid = Convert.ToInt32(Request.Params["uid"]);
            ViolationNoticeBLL vbll = new ViolationNoticeBLL(permission);
            UserBLL ubll = new UserBLL(permission);
            Staff user = ubll.GetUser(uid);
            if (user != null)
            {
                ViolationNotice pViolationNotice = new ViolationNotice();
                pViolationNotice.UserID = user.UserID;
                pViolationNotice.CompanyID = user.CompanyID;
                pViolationNotice.Address = txtAddress.Text;
                pViolationNotice.Details = txtDetails.Text;
                pViolationNotice.NoticeNo = txtNoticeNo.Text;
                pViolationNotice.ViolationDate = SwiftConverter.CovertDBObjectToDataTime(txtViolationDate.Text);

                pViolationNotice.Committer = permission.UserID;
                pViolationNotice.CommitDate = DateTime.Now;

                vbll.AddViolationNotice(pViolationNotice);

                Response.Redirect("Index.aspx");
            }
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Index.aspx");

    }
}
